package View.apply;

import Service.OutApplyService;
import Service.ServiceImpl.OutApplyServiceImpl;
import Service.ServiceImpl.stustateServiceImpl;
import Service.ServiceImpl.systemMsgServiceImpl;
import Service.stustateService;
import Service.systemMsgService;
import View.Index.Index_user;
import domain.OutApply;
import domain.User;
import domain.systemMsg;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class AddOutApply extends JFrame {


    public AddOutApply(User user){
        init();
        componentInit(user);
    }

    public void init(){
        this.setTitle("出校申请");
        this.setSize(487,400);
        this.setResizable(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
    }
    public void componentInit(User user){
        //------------------JLabel--------------------

        JLabel label3= new JLabel();
        label3.setText("外出原因:");
        label3.setBounds(137,127,65,30);

        //------------------JTextField--------------------

        JTextArea text = new JTextArea();
        text.setText("");
        text.setLineWrap(true);
        text.setBounds(216,125,200,60);
        //------------------JButton--------------------
        JButton button1= new JButton();
        button1.setText("提交");
        button1.setBounds(93,199,95,27);
        JButton button2= new JButton();
        button2.setText("退出");
        button2.setBounds(264,202,95,27);






        this.add(label3);
        this.add(text);
        this.add(button1);
        this.add(button2);

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                OutApply app = new OutApply();
                app.setStunum(user.getStunum());
                app.setName(user.getUsername());
                app.setOutReason(text.getText());
                Date date = new Date();
                app.setLeftTime(date);
                app.setCheckstate("false");
                OutApplyService service = new OutApplyServiceImpl();
                service.addApply(app);

                stustateService service1 = new stustateServiceImpl();
                String isindrom="false";
                service1.updateStudentState(app,isindrom);


                systemMsg msg = new systemMsg();
                msg.setUsername("管理员");
                msg.setTitle("学生外出申请");
                msg.setNeirong("学生:"+app.getName()+" 学号:"+app.getStunum()+"请求外出，原因:"+app.getOutReason());
                msg.setSendTime(app.getLeftTime());
                msg.setIsread("false");
                systemMsgService service2 = new systemMsgServiceImpl();
                service2.addMsg(msg);

                JOptionPane.showMessageDialog(new JFrame(),"提交成功");
            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new Index_user(user).setVisible(true);
            }
        });
    }
}
